<script setup lang="ts">
import {reactive, ref} from "vue";
import baseService from "@/service/baseService";
import SingleUpload from "@/components/jiuse/single-upload/index.vue";
import {useI18n} from "vue-i18n";
import {ElMessage} from "element-plus";

const {t} = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const dataFormRef = ref();
const loading = ref(true);

const dataForm = reactive({
  id: "",
  theme: "",
  address: "",
  activityTime: "",
  quota: 10,
  volunteer: 5,
  phone: "",
  url: ""
});

const rules = ref({
  theme: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
  address: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
  activityTime: [
    {required: true, message: '请输入', trigger: 'change'},
  ],
  quota: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
  volunteer: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
  phone: [
    {required: true, message: '请输入', trigger: 'blur'},
  ],
  url: [
    {required: true, message: '请上传', trigger: 'blur'},
  ]
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  } else {
    loading.value = false;
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService
    .get(`/clc/clceventrelease/${id}`)
    .then((res) => {
      Object.assign(dataForm, res.data);
    })
    .finally(() => {
      loading.value = false;
    });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/clc/clceventrelease", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>

<template>
  <el-drawer v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item label="活动主题" prop="theme">
        <el-input v-model="dataForm.theme" placeholder="请输入活动主题"></el-input>
      </el-form-item>
      <el-form-item label="活动地点" prop="address">
        <el-input v-model="dataForm.address" placeholder="请输入活动地点"></el-input>
      </el-form-item>
      <el-form-item label="活动时间" prop="activityTime">
        <el-date-picker
          v-model="dataForm.activityTime"
          type="datetime"
          format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="请选择活动时间"
        />
      </el-form-item>
      <el-form-item label="活动名额" prop="quota">
        <el-input-number v-model="dataForm.quota" :step="1" :min="1" :max="500"/>
      </el-form-item>
      <el-form-item label="招募志愿者数" prop="volunteer">
        <el-input-number v-model="dataForm.volunteer" :step="1" :min="0" :max="500"/>
      </el-form-item>
      <el-form-item label="报名电话" prop="phone">
        <el-input v-model="dataForm.phone" type="tel" placeholder="报名电话"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="url">
        <single-upload v-if="!loading" v-model="dataForm.url"></single-upload>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-drawer>
</template>

<style scoped lang="less">

</style>
